//
//  TabBarController.m
//  庐山通
//
//  Created by mac22 on 14/10/18.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "TabBarController.h"
#import "ZCJTabBar.h"
#import "MainController.h"
#import "ShopViewController.h"
#import "ShangMenViewController.h"
#import "SettingController.h"
#import "MeHomeViewController.h"
#import "Grouble.h"
#import "MyNavigation.h"

@interface TabBarController ()<ZCJTabBarDelegate>

@property (nonatomic, weak) ZCJTabBar *customTabBar;
@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化tabbar
    [self setupTabbar];
    
    // 初始化所有的子控制器
    [self setupAllChildViewControllers];

    // Do any additional setup after loading the view.
}
/**
 *  监听tabbar按钮的改变
 *  @param from   原来选中的位置
 *  @param to     最新选中的位置
 */
- (void)tabBar:(ZCJTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }

}
- (void)setupTabbar
{
    ZCJTabBar *customTabBar = [[ZCJTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}
/**
 *  初始化所有的子控制器
 */
- (void)setupAllChildViewControllers
{
    // 1.首页
    MainController *home = [[MainController alloc] init];
    [self setupChildViewController:home title:@"首页" imageName:@"icon_tabbar_homepage" selectedImageName:@"icon_tabbar_homepage_selected"];
    
    // 2.上门
    ShangMenViewController *shangmen = [[ShangMenViewController alloc] init];
    [self setupChildViewController:shangmen title:@"上门" imageName:@"icon_tabbar_onsite" selectedImageName:@"icon_tabbar_onsite_selected"];
    
    // 3.商家
    ShopViewController *shop = [[ShopViewController alloc] init];
    [self setupChildViewController:shop title:@"商家" imageName:@"icon_tabbar_merchant_normal" selectedImageName:@"icon_tabbar_merchant_normal_selected"];
    
    // 4.我的
    MeHomeViewController *me = [[MeHomeViewController alloc] init];
    [self setupChildViewController:me title:@"我的" imageName:@"icon_tabbar_mine" selectedImageName:@"icon_tabbar_mine_selected"];
    
    // 更多
    SettingController *setting = [[SettingController alloc] init];
    
    [self setupChildViewController:setting title:@"更多" imageName:@"icon_tabbar_misc" selectedImageName:@"icon_tabbar_misc_selected"];
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageWithName:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageWithName:selectedImageName];
    if (iOS7) {
        childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        childVc.tabBarItem.selectedImage = selectedImage;
    }
    
    // 2.包装一个导航控制器
    MyNavigation *nav = [[MyNavigation alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    // 3.添加tabbar内部的按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
